/**
 * Created by gongxun on 16/12/12.
 */

var WhmjCard = cc.Sprite.extend({
    mNumSprite : null,
    mTXSprite : null,
    mSingCountSprite : null,
    mCardNum : null,
    mKind : null,
    mCreatIndex : null,
    mCardZOrder : -1,
    ctor : function(){
        this._super();

        this.mNumSprite = new cc.Sprite();
        this.addChild(this.mNumSprite);

        this.mTXSprite = new cc.Sprite();
        this.addChild(this.mTXSprite);

        this.mSingCountSprite = new cc.Sprite();
        this.addChild(this.mSingCountSprite);
    },

    //设置牌的num
    setCardNum : function(cardNum){
        var isExist = mo.whmjCardHelper.getCardNumIsBasicLaw(cardNum);
        if (isExist){
            this.mCardNum = cardNum;
        }else{
            this.mCardNum = 0;
        }
    },

    //设置单杠牌的数量
    setSingCardCount : function (seat, count) {

        if(count < 1 || count > 4){
            return;
        }

        this.mSingCountSprite.setVisible(true);
        this.mSingCountSprite.setRotation(0);
        this.mSingCountSprite.setPosition(cc.p(0, 0));

        if (seat == 0 || seat == 2){
            this.mSingCountSprite.setSpriteFrame("cardBg1_hand1_sing"+ count +".png");
        }else{
            this.mSingCountSprite.setSpriteFrame("cardBg1_left_sing"+ count +".png");
        }

        var size = this.getContentSize();
        if(seat == 3){
            this.mSingCountSprite.setScale(-1, -1);
            this.mSingCountSprite.setPositionX(this.mSingCountSprite.getPositionX() + size.width);
            this.mSingCountSprite.setPositionY(this.mSingCountSprite.getPositionY() + size.height + 32);
        }else if(seat == 1){
            this.mSingCountSprite.setScale(-1, -1);
            this.mSingCountSprite.setPositionX(this.mSingCountSprite.getPositionX() + size.width);
            this.mSingCountSprite.setPositionY(this.mSingCountSprite.getPositionY() + size.height + 32);
        }else{
            this.mSingCountSprite.setScale(1, 1);
        }

        this.mSingCountSprite.setAnchorPoint(cc.p(0, 0));
    },

    //牌的类型 kind 1:inhand 2:deal 3:openShow 4:openHide
    setCardKind : function(seat, kind){
        this.mKind = kind;

        //还原
        this.mCardZOrder = -1;
        this.setScale(1);
        this.mNumSprite.setScale(1);

        if (kind == 1){
            //inhand
            if (seat == 0){

                this.setSpriteFrame(this.getImageFileName("hand.png"));
                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, -10));
                this.setScale(mo.whmjCardHelper.inhandCardScale);

            }else if (seat == 1){
                this.setScale(-mo.whmjCardHelper.leftInhandCardScale, mo.whmjCardHelper.leftInhandCardScale);
                this.mNumSprite.setScale(1, -1);

                if (this.mCardNum == 0){
                    this.setSpriteFrame(this.getImageFileName("leftHand.png"));
                    this.mNumSprite.setVisible(false);
                }else{
                    this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                    this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    this.setNumSpritePos(cc.p(0, 14));
                }

            }else if (seat == 2){
                this.setScale(mo.whmjCardHelper.upInhandCardScale);

                if (this.mCardNum == 0){
                    this.setSpriteFrame(this.getImageFileName("handHide.png"));
                    this.mNumSprite.setVisible(false);
                }else{
                    this.setSpriteFrame(this.getImageFileName("hand1.png"));
                    this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    this.setNumSpritePos(cc.p(0, 15));
                }

            }else if (seat == 3){
                this.setScale(mo.whmjCardHelper.leftInhandCardScale);

                if (this.mCardNum == 0){
                    this.setSpriteFrame(this.getImageFileName("leftHand.png"));
                    this.mNumSprite.setVisible(false);
                }else{
                    this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                    this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                    this.mNumSprite.setVisible(true);
                    this.setNumSpritePos(cc.p(0, 14));
                }

            }

        }else if (kind == 2){
            //deal
            if(seat == 0){
                this.setScale(mo.whmjCardHelper.dealScale);
                this.setSpriteFrame(this.getImageFileName("hand1.png"));
                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 20));
            }else if(seat == 1){
                this.setScale(mo.whmjCardHelper.leftDealScale);
                this.mNumSprite.setScale(1, 1);

                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 14));
            }else if(seat == 2){
                this.setScale(mo.whmjCardHelper.upDealScale);
                this.setSpriteFrame(this.getImageFileName("hand1.png"));
                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 20));
            }else if(seat == 3){
                this.setScale(mo.whmjCardHelper.leftDealScale);
                this.mNumSprite.setScale(1, 1);

                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 14));
            }

        }else if (kind == 3){
            //openShow

            if (seat == 0){
                this.setScale(mo.whmjCardHelper.openCardScale);

                this.setSpriteFrame(this.getImageFileName("hand1.png"));
                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 20));

            }else if (seat == 1){
                this.setScale(mo.whmjCardHelper.leftOpenCardScale);

                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 14));
            }else if (seat == 2){

                this.setScale(mo.whmjCardHelper.upOpenCardScale);
                this.setSpriteFrame(this.getImageFileName("hand1.png"));
                this.mNumSprite.setSpriteFrame("me_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 20));

            }else if (seat == 3){
                this.setScale(mo.whmjCardHelper.leftOpenCardScale);

                this.setSpriteFrame(this.getImageFileName("leftOpen.png"));
                this.mNumSprite.setSpriteFrame("out_left_" + this.mCardNum + ".png");
                this.mNumSprite.setVisible(true);
                this.setNumSpritePos(cc.p(0, 14));

            }

        }else if (kind == 4){

            //hide
            if (seat == 0){

                this.setScale(mo.whmjCardHelper.openCardScale);
                this.setSpriteFrame(this.getImageFileName("hide.png"));
                this.mNumSprite.setVisible(false);
                this.setNumSpritePos(cc.p(0, 0));

            }else if (seat == 1){
                this.setScale(mo.whmjCardHelper.leftOpenCardScale);
                this.mNumSprite.setScale(1, -1);

                var scaleX = this.getScaleX();
                var scaleY = this.getScaleY();
                this.setScale(scaleX * 0.95, scaleY);

                this.setSpriteFrame(this.getImageFileName("leftHide.png"));
                this.mNumSprite.setVisible(false);

            }else if (seat == 2){

                this.setScale(mo.whmjCardHelper.upOpenCardScale);

                this.setSpriteFrame(this.getImageFileName("hide.png"));
                this.mNumSprite.setVisible(false);
                this.setNumSpritePos(cc.p(0, 0));

            }else if (seat == 3){
                this.setScale(mo.whmjCardHelper.leftOpenCardScale);
                this.mNumSprite.setScale(1, 1);

                var scaleX = this.getScaleX();
                var scaleY = this.getScaleY();
                this.setScale(scaleX * 0.95, scaleY);

                this.setSpriteFrame(this.getImageFileName("leftHide.png"));
                this.mNumSprite.setVisible(false);
            }
        }

        this.setLaiziTx(seat, kind);
        this.setAnchorPoint(cc.p(0, 0));
    },

    setNumSpritePos : function(p) {
        var size = this.getContentSize();
        this.mNumSprite.setPosition(cc.p(size.width / 2 + p.x, size.height / 2 + p.y));

        var scaleX = this.mNumSprite.getScaleX();
        var scaleY = this.mNumSprite.getScaleY();
        this.mNumSprite.setScale(scaleX * 0.95, scaleY * 0.95);
    },

    getImageFileName : function(name){
        return "cardBg1_" + name;
    },

    //返回zorder
    getCardZ : function(){
        var z = 0;
        if (this.mCardZOrder < 0){
            if (this.mKind == 2){
                z = parseInt(mo.gameSize.y - this.getPositionY()) + mo.gameSize.y;
            }else{
                z = parseInt(mo.gameSize.y - this.getPositionY());
            }
        }else{
            z = this.mCardZOrder;
        }
        if (z == undefined){
            cc.log("sss");
        }
        return z;
    },

    //设置赖子tx
    setLaiziTx : function(seat, kind){
        this.mTXSprite.setVisible(false);
        this.mTXSprite.setRotation(0);
        this.mTXSprite.setScale(1);
        this.mTXSprite.setPosition(cc.p(0, 0));

        var carSize = mo.whmjCardHelper.inhandCardSize;

        if (seat == 0 && kind == 1){
            if (this.mCardNum != 0){
                if (this.mCardNum == mo.whmjCardHelper.laiziNum){
                    //是赖子
                    this.mTXSprite.setVisible(true);
                    this.mTXSprite.setSpriteFrame("flag_laizi.png");
                    this.mTXSprite.setPosition(cc.p(0, carSize.height - 30));
                }else if (mo.whmjCardHelper.getIsPizi(this.mCardNum)){
                    //是皮子
                    this.mTXSprite.setVisible(true);
                    this.mTXSprite.setSpriteFrame("flag_pizi.png");
                    this.mTXSprite.setPosition(cc.p(0, carSize.height - 30));
                }
            }
        }

        this.mTXSprite.setAnchorPoint(cc.p(0, 1));
    },

    //透明度
    setOpacity : function(opacity){
        this._super(opacity);

        this.mNumSprite.setOpacity(opacity);
        this.mTXSprite.setOpacity(opacity);
        this.mSingCountSprite.setOpacity(opacity);
    },

    setCardColor : function(color){
        //todo
        // this.mIsColor = true;
        // this.setColor(color);
        // this.mNumSprite.setColor(color);
        // this.mTXSprite.setColor(color);
    },
});